import Vue from 'vue'
import Vuex from 'vuex'
import { getMyShoppingCarNum } from '../api/home'
import { getUserInfo } from '../api/login';
import { getToken } from "../utils/cookie";
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userShoppingCarNum: 0,
    userInfo: null,
    isLogin: false,
  },
  //相当于computed
  getters: {
    getUserShopNum(state) {
      return state.userShoppingCarNum;
    },
  },
  mutations: {
    getLoginState(state) {
      if (getToken('token') == null) {
        state.isLogin = false;
      } else {
        state.isLogin = true;
      }
    },
    setLoginState(state) {
      state.isLogin = !state.isLogin;
    }
  },
  actions: {
    getUserInfo(context) {
      getMyShoppingCarNum().then(res => {
        context.state.userShoppingCarNum = res.data;
      })
    },
    getUsername(context) {
      getUserInfo().then(res => {
        context.commit("getLoginState");
        context.state.userInfo = res.data;
      })
    },

  },
  modules: {
  }
})
